using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.CompilerServices;
using ns20;

namespace Newtonsoft.Json.Linq
{
	public static class Extensions
	{
		[CompilerGenerated]
		private static Func<JObject, IEnumerable<JProperty>> func_0;

		public static IJEnumerable<JToken> Ancestors<T>(this IEnumerable<T> source) where T : JToken
		{
			Class204.smethod_2(source, "source");
			return source.SelectMany((T gparam_0) => gparam_0.Ancestors()).AsJEnumerable();
		}

		public static IJEnumerable<JToken> Descendants<T>(this IEnumerable<T> source) where T : JContainer
		{
			Class204.smethod_2(source, "source");
			return source.SelectMany((T gparam_0) => gparam_0.Descendants()).AsJEnumerable();
		}

		public static IJEnumerable<JProperty> Properties(this IEnumerable<JObject> source)
		{
			Class204.smethod_2(source, "source");
			return source.SelectMany((JObject jobject_0) => jobject_0.Properties()).AsJEnumerable();
		}

		public static IJEnumerable<JToken> Values(this IEnumerable<JToken> source, object key)
		{
			return source.smethod_0<JToken, JToken>(key).AsJEnumerable();
		}

		public static IJEnumerable<JToken> Values(this IEnumerable<JToken> source)
		{
			return source.Values(null);
		}

		public static IEnumerable<U> Values<U>(this IEnumerable<JToken> source, object key)
		{
			return source.smethod_0<JToken, U>(key);
		}

		public static IEnumerable<U> Values<U>(this IEnumerable<JToken> source)
		{
			return source.smethod_0<JToken, U>(null);
		}

		public static U Value<U>(this IEnumerable<JToken> value)
		{
			return value.Value<JToken, U>();
		}

		public static U Value<T, U>(this IEnumerable<T> value) where T : JToken
		{
			Class204.smethod_2(value, "source");
			JToken jToken = value as JToken;
			if (jToken == null)
			{
				throw new ArgumentException("Source value must be a JToken.");
			}
			return jToken.smethod_2<JToken, U>();
		}

		internal static IEnumerable<U> smethod_0<T, U>(this IEnumerable<T> ienumerable_0, object object_0) where T : JToken
		{
			Class204.smethod_2(ienumerable_0, "source");
			foreach (T item in ienumerable_0)
			{
				if (object_0 == null)
				{
					if (!(item is JValue))
					{
						foreach (JToken item2 in item.Children())
						{
							yield return item2.smethod_2<JToken, U>();
						}
					}
					else
					{
						yield return ((JValue)(object)item).smethod_2<JValue, U>();
					}
				}
				else
				{
					JToken jToken = item[object_0];
					if (jToken != null)
					{
						yield return jToken.smethod_2<JToken, U>();
					}
				}
			}
		}

		public static IJEnumerable<JToken> Children<T>(this IEnumerable<T> source) where T : JToken
		{
			return source.Children<T, JToken>().AsJEnumerable();
		}

		public static IEnumerable<U> Children<T, U>(this IEnumerable<T> source) where T : JToken
		{
			Class204.smethod_2(source, "source");
			return source.SelectMany((T gparam_0) => gparam_0.Children()).smethod_1<JToken, U>();
		}

		internal static IEnumerable<U> smethod_1<T, U>(this IEnumerable<T> ienumerable_0) where T : JToken
		{
			Class204.smethod_2(ienumerable_0, "source");
			foreach (T item in ienumerable_0)
			{
				yield return item.smethod_2<JToken, U>();
			}
		}

		internal static U smethod_2<T, U>(this T gparam_0) where T : JToken
		{
			if (gparam_0 == null)
			{
				return default(U);
			}
			if (gparam_0 is U && typeof(U) != typeof(IComparable) && typeof(U) != typeof(IFormattable))
			{
				return (U)(object)gparam_0;
			}
			JValue jValue = gparam_0 as JValue;
			if (jValue == null)
			{
				throw new InvalidCastException("Cannot cast {0} to {1}.".smethod_1(CultureInfo.InvariantCulture, gparam_0.GetType(), typeof(T)));
			}
			if (jValue.Value is U)
			{
				return (U)jValue.Value;
			}
			Type type = typeof(U);
			if (Class195.smethod_10(type))
			{
				if (jValue.Value == null)
				{
					return default(U);
				}
				type = Nullable.GetUnderlyingType(type);
			}
			return (U)Convert.ChangeType(jValue.Value, type, CultureInfo.InvariantCulture);
		}

		public static IJEnumerable<JToken> AsJEnumerable(this IEnumerable<JToken> source)
		{
			return source.AsJEnumerable<JToken>();
		}

		public static IJEnumerable<T> AsJEnumerable<T>(this IEnumerable<T> source) where T : JToken
		{
			if (source == null)
			{
				return null;
			}
			if (source is IJEnumerable<T>)
			{
				return (IJEnumerable<T>)source;
			}
			return new JEnumerable<T>(source);
		}

		[CompilerGenerated]
		private static IEnumerable<JToken> smethod_3<T>(T gparam_0) where T : JToken
		{
			return gparam_0.Ancestors();
		}

		[CompilerGenerated]
		private static IEnumerable<JToken> smethod_4<T>(T gparam_0) where T : JContainer
		{
			return gparam_0.Descendants();
		}

		[CompilerGenerated]
		private static IEnumerable<JProperty> smethod_5(JObject jobject_0)
		{
			return jobject_0.Properties();
		}

		[CompilerGenerated]
		private static IEnumerable<JToken> smethod_6<T, U>(T gparam_0) where T : JToken
		{
			return gparam_0.Children();
		}
	}
}
